<template>
  <view class="whole">
    <view class="status_bar"></view>

    <u-toast ref="uToast" />

    <view class="title">
      <image
        src="../../static/jiantous.png"
        mode=""
        class="title_image"
        @click="handleBack"
      ></image>
      <text>绑定银行卡</text>

      <view class="title_right" @click="handleDelete">删除</view>
    </view>

    <view class="password_top">
      <view
        class="password"
        style="display: flex; justify-content: flex-start; align-items: center"
      >
        <input
          placeholder="输入开户姓名"
          v-model="name"
          style="font-size: 28rpx; flex: 2"
          placeholder-style="font-size: 28rpx;"
        />
      </view>
      <view
        style="height: 2rpx; background-color: #313535; margin: 0 30rpx"
      ></view>

      <view class="password">
        <input
          placeholder="输入身份证号，认真核对避免出错"
          v-model="idcard"
          style="font-size: 28rpx"
          placeholder-style="font-size: 28rpx;"
        />
      </view>
      <view
        style="height: 2rpx; background-color: #313535; margin: 0 30rpx"
      ></view>

      <view
        class="password"
        style="display: flex; justify-content: flex-start; align-items: center"
      >
        <input
          placeholder="输入开户银行且具体到支行，否则无法汇款"
          v-model="bank_name"
          style="font-size: 28rpx; flex: 2"
          placeholder-style="font-size: 28rpx;"
        />
      </view>
      <view
        style="height: 2rpx; background-color: #313535; margin: 0 30rpx"
      ></view>

      <view class="password">
        <input
          placeholder="输入银行卡号"
          v-model="bankcard"
          style="font-size: 28rpx"
          placeholder-style="font-size: 28rpx;"
        />
      </view>
      <view
        style="height: 2rpx; background-color: #313535; margin: 0 30rpx"
      ></view>

      <view class="passwords">
        <input
          type="number"
          placeholder="输入银行卡预留手机号"
          v-model="bankcardphone"
          style="font-size: 28rpx; flex: 2"
          placeholder-style="font-size: 28rpx;"
        />
        <!-- <u-verification-code :seconds="seconds"   ref="uCode" @change="codeChange"></u-verification-code>
				<view style="display: flex;align-items: center;">
					<view style="width: 2rpx;height: 30rpx;background-color: #F6F8FA;margin-right: 15rpx;"></view>
					<text @click="getCode" style="color: #39A2FF;font-size: 28rpx;white-space: normal" >{{tips}}</text>
				</view> -->
      </view>
    </view>

    <!-- <view class="password_top" style="margin-top: 30rpx;">
			<view class="password">
				<input type="password" placeholder="请填写获取的验证码" v-model="phoneCode" style="font-size: 28rpx;" placeholder-style="font-size: 28rpx;">
			</view>
			<view style="height: 2rpx;background-color: #F6F8FA;margin: 0  30rpx;"></view>
			
		</view> -->

    <view class="button" @click="Submit"> 确定 </view>
  </view>
</template>

<script>
import api from "../../api/api";
import tip from "@/utils/tip.js";
export default {
  data() {
    return {
      name: "",
      bankcardphone: "",
      idcard: "",
      bankcard: "",
      phoneCode: "",
      tips: "",
      // refCode: null,
      seconds: 60,
      timer: false,
      ordernumber: "",
      id: "",
      bank_name: "",
    };
  },
  onLoad() {
    let alipaylist = JSON.parse(uni.getStorageSync("walletcard"));
    alipaylist.forEach((el) => {
      if (el.id_number) {
        this.name = el.real_name;
        this.bankcard = el.bank_card_number;
        this.bankcardphone = el.bank_card_phone;
        this.idcard = el.id_number;
        this.id = el.id;
        this.bank_name = el.bank_name;
      }
    });
  },
  methods: {
    codeChange(text) {
      this.tips = text;
    },
    // async getCode() {
    //   if (this.name == "") {
    //     this.$refs.uToast.show({
    //       title: "请填写开户姓名",
    //     });
    //     return;
    //   }
    //   if (this.idcard == "") {
    //     this.$refs.uToast.show({
    //       title: "请填写身份证号",
    //     });
    //     return;
    //   }
    //   if (this.bank_name == "") {
    //     this.$refs.uToast.show({
    //       title: "请输入银行卡开户行",
    //     });
    //     return;
    //   }
    //   if (this.bankcard == "") {
    //     this.$refs.uToast.show({
    //       title: "请填写银行卡号",
    //     });
    //     return;
    //   }
    //   if (this.bankcard.length < 16 || this.bankcard.length > 19) {
    //     this.$refs.uToast.show({
    //       title: "请填写正确银行卡号",
    //     });
    //     return;
    //   }
    //   if (this.bankcardphone == "") {
    //     this.$refs.uToast.show({
    //       title: "请填写银行卡预留手机号",
    //     });
    //     return;
    //   }
    //   if (!/^1[3456789]\d{9}$/.test(this.bankcardphone)) {
    //     this.$refs.uToast.show({
    //       title: "请填写正确手机号",
    //     });
    //     return;
    //   }
    //   let resa = await api.addPayment({
    //     query: {
    //       name: this.name,
    //       id_card_no: this.idcard,
    //       bank_no: this.bankcard,
    //       phone: this.bankcardphone,
    //       payment_id: this.id,
    //     },
    //     method: "post",
    //   });
    //   if (resa.code == 0) {
    //     this.ordernumber = res.data;
    //     this.$refs.uToast.show({
    //       title: resa.msg,
    //       type: "success",
    //     });
    //   } else {
    //     this.$refs.uToast.show({
    //       title: resa.msg,
    //       type: "error",
    //     });
    //     return;
    //   }
    //   this.timer = true;
    //   this.tips = this.seconds;
    //   var time = setInterval(() => {
    //     this.tips--;
    //     if (this.tips <= 0) {
    //       clearInterval(time);
    //       this.tips = "重新获取";
    //       this.timer = false;
    //     }
    //   }, 1000);
    // },
    async Submit() {
      if (this.name == "") {
        this.$refs.uToast.show({
          title: "输入开户姓名",
        });
        return;
      }
      if (this.idcard == "") {
        this.$refs.uToast.show({
          title: "输入身份证号，认真核对避免出错",
        });
        return;
      }
      if (this.bank_name == "") {
        this.$refs.uToast.show({
          title: "输入开户银行且具体到支行，否则无法汇款",
        });
        return;
      }
      if (this.bankcard == "") {
        this.$refs.uToast.show({
          title: "输入银行卡号",
        });
        return;
      }
      if (this.bankcard.length < 16 || this.bankcard.length > 19) {
        this.$refs.uToast.show({
          title: "输入正确的银行卡号",
        });
        return;
      }
      if (this.bankcardphone == "") {
        this.$refs.uToast.show({
          title: "输入银行卡预留手机号",
        });
        return;
      }
      if (!/^1[3456789]\d{9}$/.test(this.bankcardphone)) {
        this.$refs.uToast.show({
          title: "输入正确的手机号",
        });
        return;
      }

      let res = await api.addPayment({
        query: {
          // request_num:this.ordernumber,
          // code:this.phoneCode,
          real_name: this.name,
          wallet_id: uni.getStorageSync("walletid"),
          type: 1,
          id_number: this.idcard,
          bank_card_number: this.bankcard,
          bank_card_phone: this.bankcardphone,
          payment_id: this.id,
          bank_name: this.bank_name,
        },
        method: "post",
      });
      if (res.code == 0) {
        this.$refs.uToast.show({
          title: "绑定成功",
          type: "success",
        });

        let wallet = await api.userWallet({ method: "get" });
        uni.setStorageSync("walletid", wallet.data.id);
        let walletcard = JSON.stringify(wallet.data.payment_method);
        uni.setStorageSync("walletcard", walletcard);

        setTimeout(() => {
          uni.navigateBack({
            delta: 1,
          });
        }, 500);
      } else {
        this.$refs.uToast.show({
          title: res.msg,
          type: "error",
        });
      }
    },

    handleBack() {
      uni.navigateBack({
        delta: 1,
      });
    },

    async handleDelete() {
      let res = await api.delPayment({
        query: {
          payment_id: this.id,
        },
        method: "get",
      });

      if (res.code == 0) {
        tip.defaults("删除成功");
        uni.navigateBack({
          delta: 1,
        });
      } else {
        tip.defaults(res.msg);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/yuanchuang.scss";
.whole {
  background-color: $bg-color1;
  min-height: 100vh;
}
.status_bar {
  margin: 0 -30rpx;
  height: var(--status-bar-height);
  width: 750rpx;
}
.title {
  position: relative;
  // width: 68rpx;
  height: 88rpx;
  font-size: 34rpx;
  font-weight: 500;
  text-align: center;
  background: $bg-color1;
  color: #ffffff;
  line-height: 88rpx;
  &_image {
    position: absolute;
    left: 30rpx;
    top: 23rpx;
    width: 42rpx;
    height: 42rpx;
  }
  &_right {
    position: absolute;
    top: 0;
    right: 30rpx;
    height: 88rpx;
    font-size: 28rpx;
    font-weight: 400;
    color: $text-color3;
    line-height: 88rpx;
  }
}
.password_top {
  border: 2rpx solid $bg-color3;
  margin: 0 30rpx;
  background-color: $bg-color3;
  border-radius: 25rpx;
  position: relative;
  top: 50rpx;
  font-size: 28rpx;
}
.password {
  /* display: flex;
		justify-content: space-between;
		align-items: center; */
  color: #ffffff;
  margin: 20rpx 30rpx;
  padding: 5rpx 0;
  /* min-height: 50rpx; */
}
.passwords {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ffffff;
  margin: 20rpx 30rpx;
  min-height: 50rpx;
}
.password_center {
  position: relative;
  top: 50rpx;
  color: #999999;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin: 30rpx;
  font-size: 28rpx;
}
.button {
  position: relative;
  top: 100rpx;
  margin: 30rpx;
  height: 90rpx;
  color: $text-color4;
  background: $button;
  background-repeat: no-repeat;
  background-size: 100%;
  text-align: center;
  line-height: 90rpx;
  font-size: 32rpx;
  border-radius: 16rpx;
}
</style>
